//
//  Line.swift
//  TinyShopKeeper
//
//  Created by rimi on 16/6/7.
//  Copyright © 2016年 yuying. All rights reserved.
//

import Foundation
import UIKit

//两点连接的线
func shapeLayer(startPoint:CGPoint,endPoint:CGPoint) -> CAShapeLayer{
    
    let shapeLayer = CAShapeLayer()
    //path 路径 形状根据路径决定
    let bezierPath = UIBezierPath()
    //移动到开始的地方
    bezierPath.moveToPoint(startPoint)
    //添加一条线到一个点
    bezierPath.addLineToPoint(endPoint)
    shapeLayer.path = bezierPath.CGPath
    //线条颜色
    shapeLayer.strokeColor = ApplicationTintColor.CGColor
    //线宽
    shapeLayer.lineWidth = 4
    shapeLayer.shadowColor = DefaultGrayColor.CGColor//阴影颜色
    shapeLayer.shadowOffset = CGSizeMake(2, 1)//阴影偏移
    shapeLayer.shadowOpacity = 1//阴影不透明度
    
    //给layer添加动画
    let basicAnimation = CABasicAnimation(keyPath: "strokeEnd")
    basicAnimation.duration = 0.5
    basicAnimation.timingFunction = CAMediaTimingFunction(name: "linear")
    //设置动画结束时 layer 保持动画结束时的状态
    basicAnimation.fillMode = "forwards"
    //当动画结束不移除动画
    basicAnimation.removedOnCompletion = false
    //动画开始值
    basicAnimation.fromValue = NSNumber(int: 0)
    //动画结束值
    basicAnimation.toValue = NSNumber(int: 1)
    
    shapeLayer.addAnimation(basicAnimation, forKey: "shapLayerAnimation")
    
    return shapeLayer
}

